import { objectUploadFile } from '@/api/interface/common/file'

const uploadFile = (params: any, size = 1024 * 1024 * 2) => {
  return new Promise((resolve, reject) => {
    const file = params.file
    const returners: any = {
      fileMark: null,
      name: null,
      sort: null,
      status: null,
      url: null
    }
    const timestamp = new Date().valueOf()
    let allow = true
    // 判断文件大小
    if (size) {
      if (file.size > size) {
        allow = false
      }
    }
    if (allow) {
      objectUploadFile(file)
        .then((res: any) => {
          const result = res[0]
          returners.fileMark = timestamp
          returners.imgAddr = result.fileUrl
          returners.imgName = result.fileRealName
          returners.imgUrl = result.fileUrl
          returners.name = result.fileName
          returners.sort = 1
          returners.status = 'success'
          returners.url = result.fileUrl
          returners.msg = '上传成功'
          returners.uid = params.file.uid
          resolve(returners)
        })
        .catch((err: any) => {
          returners.status = 'failure'
          returners.msg = err
          returners.uid = params.file.uid
          reject(returners)
        })
    } else {
      let limitSize = ''
      let unit = 'KB'
      if (size / (1024 * 1024) >= 1) {
        limitSize = (size / (1024 * 1024)).toFixed(2)
        unit = 'MB'
      } else {
        limitSize = (size / 1024).toFixed(2)
        unit = 'KB'
      }
      returners.status = 'failure'
      returners.msg = `文件大小不能超过${limitSize}${unit}，请重新上传`
      returners.uid = params.file.uid
      reject(returners)
    }
  })
}

// const uploadFile = (params: any) => {
//   return new Promise((resolve, reject) => {
//     const bucketName = getBucketName()
//     const file = params.file
//     const returners: any = {
//       fileMark: null,
//       name: null,
//       sort: null,
//       status: null,
//       url: null
//     }
//     console.log('上传文件', file)
//     const s3 = new S3({
//       apiVersion: '2006-03-01',
//       accessKeyId: 'SS5KNC3BLBMLNATOWH76',
//       secretAccessKey: 'bbCHF7xJ5AKCGHkgABL5ABSZlAr9B5aZdoLYqnWA',
//       endpoint: 'eos-yunnan-2.cmecloud.cn',
//       s3ForcePathStyle: true,
//       signatureVersion: 'v2',
//       sslEnabled: false
//     })
//     const options = {
//       partSize: 10 * 1024 * 1024,
//       queueSize: 5
//     }
//     const timestamp = new Date().valueOf()
//     s3.upload(
//       {
//         Bucket: bucketName,
//         Key: timestamp + file.name.substring(file.name.lastIndexOf('.'), file.name.length),
//         Body: file,
//         ContentType: file.type,
//         ACL: 'public-read'
//       },
//       options,
//       function (err: any, data: any) {
//         if (err) {
//           returners.status = 'failure'
//           returners.msg = err
//           returners.uid = params.file.uid
//           reject(returners)
//         } else {
//           returners.fileMark = timestamp
//           returners.imgAddr = data.Location
//           returners.imgName = file.name.substring(0, file.name.lastIndexOf('.'))
//           returners.imgUrl = data.Location
//           returners.name = file.name.substring(0, file.name.lastIndexOf('.'))
//           returners.sort = 1
//           returners.status = 'success'
//           returners.url = data.Location
//           returners.msg = '上传成功'
//           returners.uid = params.file.uid
//           resolve(returners)
//         }
//       }
//     )
//   })
// }

export { uploadFile }
